package com.freecloud.framework.plugin.feign;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import feign.Util;
import feign.gson.DoubleToIntMapTypeAdapter;

import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author: maomao
 * @Date: 2019-08-19 10:56
 */
public class GsonFactory {
    public GsonFactory() {
    }

    static Gson create(Iterable<TypeAdapter<?>> adapters) {
        GsonBuilder builder = (new GsonBuilder()).setPrettyPrinting();
        builder.registerTypeAdapter((new TypeToken<Map<String, Object>>() {
        }).getType(), new DoubleToIntMapTypeAdapter());
        Iterator var2 = adapters.iterator();

        while(var2.hasNext()) {
            TypeAdapter<?> adapter = (TypeAdapter)var2.next();
            Type type = Util.resolveLastTypeParameter(adapter.getClass(), TypeAdapter.class);
            builder.registerTypeAdapter(type, adapter);
        }

        return builder.create();
    }
}
